import Vue from 'vue'
const  permissionList = JSON.parse(localStorage.getItem("button_menu"))
function checkPermission (permissionList,key) {
  let flag = false;
  for (let i = 0; i < permissionList.length; i++) {
    let n = permissionList[i].children.length;
    if (n === 0){
      flag = permissionList[i].meta.title === key ? true : false;
      if (flag === true){
        return flag
      }
    }else {
      flag = flag || checkPermission(permissionList[i].children,key);
    }
  }
  return flag;
}
const btpermission = (el, binding) => {
  let key = binding.value; //获取v-permission = '3'
  if (key) {
    let hasPermission = checkPermission(permissionList,key);
    if (!hasPermission) {
      el.disabled = true;
      el.disabled(); //没有权限移除元素
    }
  } else {
    throw new Error('需要定义key')
  }
}
const directives = {
  btpermission,
};
export default {
  install(Vue) {
    Object.keys(directives).forEach((key) => {
      Vue.directive(key, directives[key]);
    });
  },
};
// Vue.directive('btpermission', {
//   inserted (el, binding) {
//     let key = binding.value; //获取v-permission = '3'
//     if (key) {
//       let hasPermission = checkPermission(permissionList,key);
//       if (!hasPermission) {
//         el.disabled = true;
//         el.disabled(); //没有权限移除元素
//       }
//     } else {
//       throw new Error('需要定义key')
//     }
//   }
// })
